package cn.dankal.flyaudio.friend;

import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.util.SimpleTextChange;
import cn.dankal.basiclib.util.SpannableStringUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.ClearEditText;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.VerifyFriendAdapter;
import cn.dankal.flyaudio.database.FriendRecommendEntry;
import cn.dankal.flyaudio.database.UserEntry;
import cn.dankal.flyaudio.entity.GroupEntity;
import cn.dankal.flyaudio.friend.utils.GroupManagerUtils;
import cn.dankal.flyaudio.friend.utils.pinyin.HanziToPinyin;
import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoListCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.UserInfo;

public class NewFriendActivity extends BaseActivity implements View.OnClickListener {

    private EditText searchView;

    private VerifyFriendAdapter adapter;

    private RecyclerView recyclerView;

    private View addGroupView;

    private View addGroupByFaceView;

    private MultipleStatusView statusView;

    private TextView titleView;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_friend;
    }

    @Override
    protected void initComponents() {
        searchView = findViewById(R.id.et_search);
        recyclerView = findViewById(R.id.recyclerview);
        addGroupByFaceView = findViewById(R.id.rl_create_group_by_face);
        titleView = findViewById(R.id.tv_title);
        addGroupView = findViewById(R.id.rl_create_group);
        statusView = findViewById(R.id.multiple_status_view);
        addGroupByFaceView.setVisibility(View.GONE);
        addGroupView.setVisibility(View.GONE);
        findViewById(R.id.tv_search).setOnClickListener(this);
        adapter = new VerifyFriendAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        recyclerView.setVisibility(View.VISIBLE);
        titleView.setText("新的朋友");
        statusView.setVisibility(View.VISIBLE);
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initSearch();
        initData();

    }

    private List<FriendRecommendEntry> friendList = new ArrayList<>();
    private TextView searchTextView;

    private ClearEditText searchEditText;

    private void initSearch() {
        statusView = findViewById(R.id.multiple_status_view);
        searchTextView = findViewById(R.id.tv_search);
        searchEditText = findViewById(R.id.et_search);
        searchEditText.setOnSearchListener(new ClearEditText.OnSearchListener() {
            @Override
            public void doSearch(String keyword) {
                if (TextUtils.isEmpty(keyword)) return;
                mDoSearch(keyword);
            }
        });
        searchTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = searchEditText.getText().toString();
                if (TextUtils.isEmpty(key)) return;
                mDoSearch(key);
            }
        });
        searchEditText.addTextChangedListener(new SimpleTextChange() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                super.onTextChanged(s, start, before, count);
                if (TextUtils.isEmpty(s)) {
                    statusView.showContent();
                    adapter.addDatas(friendList,true);
                }
            }
        });
        statusView.showContent();
    }

    private void mDoSearch(String key) {
        List<FriendRecommendEntry> searResult = new ArrayList<>();
        for (FriendRecommendEntry userInfo : friendList) {
            ArrayList<HanziToPinyin.Token> tokens = HanziToPinyin.getInstance()
                    .get(userInfo.nickName);
            StringBuilder sb = new StringBuilder();
            if (tokens != null && tokens.size() > 0) {
                for (HanziToPinyin.Token token : tokens) {
                    if (token.type == HanziToPinyin.Token.PINYIN) {
                        sb.append(token.target);
                    } else {
                        sb.append(token.source);
                    }
                }
            }
            if (sb.toString().contains(key) || userInfo.nickName.contains(key)) {
                searResult.add(userInfo);
            }
        }
        if (searResult.isEmpty()) {
            SpannableStringBuilder s =
                    SpannableStringUtils.getBuilder(key).setForegroundColor(getResources().getColor(R.color.color_0085B4)).create();

            statusView.showEmpty("- 没有找到“" + s + "”相关结果 -");

//            findViewById(R.id.status_hint_content_1).setVisibility(View.GONE);
        } else {
            statusView.showContent();
//            findViewById(R.id.status_hint_content_1).setVisibility(View.VISIBLE);
            adapter.addDatas(friendList, true);
//            adapter.setNewData(searResult, true);
//            friendView.setAdapter(searchUserAdapter);
        }
    }


    private void initData() {
        UserEntry user = FlyApplication.getUserEntry();
        if (null != user) {
            List<FriendRecommendEntry> mList = user.getRecommends();
            friendList.addAll(mList);
            ContactManager.getFriendList(new GetUserInfoListCallback() {
                @Override
                public void gotResult(int i, String s, List<UserInfo> list) {
                    StringBuilder userNames = new StringBuilder();
                    for (UserInfo userInfo : list) {
                        userNames.append(userInfo.getUserName());
                        userNames.append(",");
                    }
                    String userNameKeys = userNames.toString();
                    for (FriendRecommendEntry entry : mList) {
                        if (userNameKeys.contains(entry.username)) {
                            entry.btnState = 1;
                        }
                    }
                    adapter.addDatas(mList, true);
                    statusView.showContent();
                }
            });
//            mAdapter = new FriendRecommendAdapter(this, mList, mDensity, mWidth);
//            mListView.setAdapter(mAdapter);
        } else {
            statusView.showEmpty();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.tv_search == id) {
            recyclerView.setVisibility(View.VISIBLE);
            List<String> list = new ArrayList<>();
            list.add(searchView.getText().toString());
//            adapter.addDatas(list, true);
        }
    }
}
